//
//  EMWIFITextView.m
//  WIFI
//
//  Created by linnzh on 2023/4/12.
//

#import "EMWIFITextView.h"

@interface EMWIFITextView ()

@property (nonatomic ,strong) UILabel *placeHolderLabel;

@end

@implementation EMWIFITextView

#pragma mark - Life cycle
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(didReceiveTextDidChangeNotification:)
                                                     name:UITextViewTextDidChangeNotification
                                                   object:self];
        self.placeHolderLabel.frame = CGRectMake(0, 10, frame.size.width, frame.size.height-20);
//        [self placeAndLayoutSubviews];
    }
    return self;
}

//- (void)placeAndLayoutSubviews {
//    [self addSubview:self.placeHolderLabel];
//    [self.placeHolderLabel mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.mas_equalTo(self).offset(10.0);
//        make.left.mas_equalTo(self).offset(0.0);
//        make.right.mas_equalTo(self).offset(-13);
//    }];
//}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UITextViewTextDidChangeNotification object:self];
}

#pragma mark - Notifications
- (void)didReceiveTextDidChangeNotification:(NSNotification *)notification {
    self.placeHolderLabel.hidden = self.text.length > 0 ? YES : NO;
}


#pragma mark getter and setter
- (UILabel *)placeHolderLabel {
    if (_placeHolderLabel == nil) {
        _placeHolderLabel = [[UILabel alloc] init];
        _placeHolderLabel.font = self.font;
        _placeHolderLabel.textColor = [UIColor lightGrayColor];
        _placeHolderLabel.textAlignment = NSTextAlignmentLeft;
        _placeHolderLabel.lineBreakMode = NSLineBreakByTruncatingTail;
        _placeHolderLabel.numberOfLines = 0;
    }
    return _placeHolderLabel;
}

- (void)setPlaceHolder:(NSString *)placeHolder {
    _placeHolder = placeHolder;
    self.placeHolderLabel.text = _placeHolder;
}

- (void)setPlaceHolderColor:(UIColor *)placeHolderColor {
    _placeHolderColor = placeHolderColor;
    self.placeHolderLabel.textColor = _placeHolderColor;
}

@end
